$(() => {
    // 判断是否登录，否则跳转登录页，是则跳转我的页面
    $("#myself").click(() => {
        window.location.href = "./mine/index.html";
    })
    // 特惠度假tab选择
    $(".classify").children().map((i, v) => {
        $(v).attr("id", i);
        $(v).click(() => {
            $(v).addClass("active").siblings().removeClass("active");
            if($(v).hasClass("active") && $(v).attr("id") == 0){
                // console.log($(v))
                $(".address").show();
                $(".today").hide();
            }else if($(v).hasClass("active") && $(v).attr("id") == 1){
                $(".today").show();
                $(".address").hide();
            }
        })
    })

    let info1 = localStorage.getItem("info1");
    let info2 = localStorage.getItem("info2");
    let loadFlag = $(".loading");
    let more = $(".more");
    if(!!info1 && !!info2){
        dataInfo = JSON.parse(info1);
        dataInfo.map((v,i) => {
            if(i < 8){
                let sights = !!v.sights[0] ? v.sights[0] : v.ttsRouteType;
                let type = v.type == "freetrip" ? "自由行" : "跟团游";
                let element = `
                    <li>
                        <a href="../pages/details/index.html?id=${v.encodeId}">
                            <div class="img">
                                <div class="flag">${type}</div>
                                <img src="${v.extensionImg}" alt="">
                                <div class="desc">${sights}</div>
                            </div>
                            <div class="title">${v.title}</div>
                            <div class="price">￥ ${v.totalPrice} <span style="color: #e8e8;">起</span></div>
                            <div class="data">出发日期：${v.details.date}</div>
                        </a>
                    </li>
                `;
                // let createdLi = ali.clone(true)[0];
                // console.log( createdLi.childNodes[1].childNodes[1])
                // createdLi.childNodes[1].childNodes[1].src = v.imgurl;
                $(element).appendTo(".address>ul");
            }
        })
        more.css("display", "block");
        JSON.parse(info2).map(v => {
            let query = v.url.split("?")[1];
            let element = `
                <li>
                    <a href="../pages/details/index.html?${query}">
                        <div class="img">
                            <div class="flag">${v.function}</div>
                            <img src="${v.image}" alt="">
                            <div class="desc">${v.title}</div>
                        </div>
                        <div class="title">${v.desc}</div>
                        <div class="price">￥ ${v.orgPrice} <span style="color: #e8e8;">起</span></div>
                        <div class="data">期限：${v.dayAndNight}</div>
                    </a>
                </li>
            `;
            $(element).appendTo(".today>ul");
        })
        loadFlag.css("display", "none");
    }else{
        // 根据定位获得当前城市景点数据
        let currentCity = null;
        AMap.plugin('AMap.Geolocation', function() {
            var geolocation = new AMap.Geolocation({
                // 是否使用高精度定位，默认：true
                enableHighAccuracy: true,
                // 设置定位超时时间，默认：无穷大
                timeout: 10000,
                // 定位按钮的停靠位置的偏移量，默认：Pixel(10, 20)
                buttonOffset: new AMap.Pixel(10, 20),
                //  定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
                zoomToAccuracy: true,     
                //  定位按钮的排放位置,  RB表示右下
                buttonPosition: 'RB'
            })
        
            geolocation.getCurrentPosition()
            AMap.event.addListener(geolocation, 'complete', onComplete)
            AMap.event.addListener(geolocation, 'error', onError)
        
            function onComplete (data) {
                // data是具体的定位信息
                console.log("data =>",data);
                currentCity = data.addressComponent.city;
                getCurrentCityData();
            }
        
            function onError (data) {
            // 定位出错
            }
        });

        // 景点数据请求
        var dataInfo = null;
        let getCurrentCityData = () => {
            $.ajax({
                url: 'https://dujia.qunar.com/golfz/routeList',
                type: 'GET',
                data: {
                    isTouch: 0,
                    o: 'pop-desc',
                    lm: '0,40',
                    qs_ts: 1615424663853,
                    sourcepage: 'around',
                    d: currentCity,
                    q: currentCity,
                    tf: 'dj_aroundnav',
                    tm: 'djnull',
                    random: 753696,
                    t: 'all',
                    qssrc: 'eyJ0cyI6IjE2MTU0MjQ2NjM4NTMiLCJzcmMiOiJhcm91bmQuZW52YW5vIiwiYWN0Ijoic2VhcmNoIiwicmFuZG9tIjoiNzUzNjk2In0=',
                    m: 'l,lm',
                    displayStatus: 'pc',
                    i: '1,2,3,4,5',
                    width: 304,
                    height: 201,
                    linesType: '周边游,本地游',
                    fl: 'url,price,totalPrice,taocanPrice,originalPrice,type,multi_price,local,extensionImg,sales,details,tripTimeTitle,allDate,summary,dep,title,sights,arrive,ttsRouteType,citys,virtual,virtual_num,hotel_title,subtitle,id,encodeId',
                },
                dataType: "jsonp",
                beforeSend: function(){
                    loadFlag.css("display", "block");
                    console.log('请求前');
                },
                success: function (data) { 
                    let info1 = data.data.list.results;
                    dataInfo = info1;
                    info1.map((v,i) => {
                        if(i < 8){
                            let sights = !!v.sights[0] ? v.sights[0] : v.ttsRouteType;
                            let type = v.type == "freetrip" ? "自由行" : "跟团游";
                            let element = `
                                <li>
                                    <a href="../pages/details/index.html?id=${v.encodeId}">
                                        <div class="img">
                                            <div class="flag">${type}</div>
                                            <img src="${v.extensionImg}" alt="">
                                            <div class="desc">${sights}</div>
                                        </div>
                                        <div class="title">${v.title}</div>
                                        <div class="price">￥ ${v.totalPrice} <span style="color: #e8e8;">起</span></div>
                                        <div class="data">出发日期：${v.details.date}</div>
                                    </a>
                                </li>
                            `;
                            // let createdLi = ali.clone(true)[0];
                            // console.log( createdLi.childNodes[1].childNodes[1])
                            // createdLi.childNodes[1].childNodes[1].src = v.imgurl;
                            $(element).appendTo(".address>ul");
                        }
                    })
                    localStorage.setItem("info1",JSON.stringify(info1));
                    loadFlag.css("display", "none");
                    more.css("display", "block");
                },
                error: function(err1){
                    console.log(err1);
                }
            });

            $.ajax({
                url: 'https://dujia.qunar.com/golfz/destination/tejiaLeft',
                type: 'GET',
                data: {
                    num: 8,
                    tms: 'pc_jrth',
                    dep: currentCity.substr(0,currentCity.length-1),
                    // callback: 'jQuery172005274975770125434_1615446910616',
                    _: '1615446923445',
                },
                dataType: "jsonp",
                beforeSend: function(){
                    loadFlag.css("display", "block");
                },
                success: function(data){
                    console.log(data);
                    let info2 = data.data;
                    info2.map(v => {
                        let query = v.url.split("?")[1];
                        let element = `
                            <li>
                                <a href="../pages/details/index.html?${query}">
                                    <div class="img">
                                        <div class="flag">${v.function}</div>
                                        <img src="${v.image}" alt="">
                                        <div class="desc">${v.title}</div>
                                    </div>
                                    <div class="title">${v.desc}</div>
                                    <div class="price">￥ ${v.orgPrice} <span style="color: #e8e8;">起</span></div>
                                    <div class="data">期限：${v.dayAndNight}</div>
                                </a>
                            </li>
                        `;
                        $(element).appendTo(".today>ul");
                    })
                    localStorage.setItem("info2",JSON.stringify(info2));
                    loadFlag.css("display", "none");
                },
                error: function(err2){
                    console.log(err2);
                }
            })
        }
    }

    // 更多数据页面跳转
    $(".more").click(() => {
        let dataInfo_ = JSON.stringify(dataInfo);
        localStorage.setItem("dataInfo",dataInfo_);
        window.location.href = `../pages/more/index.html`;
    })

})
